package com.qf.rabbitmqcase.config;

import com.qf.rabbitmqcase.constant.Constant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 刘欣
 * @createTime 2023/3/3 13:45
 * @description
 */
@Configuration
public class RabbitMqConfig {


    //声明交换机
    @Bean("topicexchange")
    public TopicExchange getTopicExchange() {
        TopicExchange exchange = new TopicExchange(Constant.NORMAL_EXCHANGE_NAME, true, false);
        return exchange;
    }

    //声明延时对列
    @Bean("delayedqueue")
    public Queue getDelayedQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", Constant.DEAD_LETTER_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key", Constant.DEAD_LETTER_QUEUE_10S_ROUTINGKEY);
        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable(Constant.DELAY_QUEUE_NAME).withArguments(args).build();
    }


    //声明死信交换机
    @Bean("directexchange")
    public DirectExchange getdirectExchange() {
        DirectExchange exchange = new DirectExchange(Constant.DEAD_LETTER_EXCHANGE_NAME, true, false);
        return exchange;
    }

    //声明死信队列
    @Bean("deadqueue")
    public Queue deadQueue() {
        Queue dead_queue = new Queue(Constant.DEAD_LETTER_QUEUE_NAME);
        return dead_queue;
    }

    //绑定交换机与延时队列
    @Bean("normalbinding")
    public Binding normalBinding(TopicExchange topicexchange, Queue delayedqueue) {
        return BindingBuilder.bind(delayedqueue).to(topicexchange).with(Constant.DELAY_QUEUE_ROUTINGKEY);
    }


    //绑定死信交换机与死信队列
    @Bean("dlxbinding")
    public Binding dxlBinding(DirectExchange directexchange, Queue deadqueue) {
        return BindingBuilder.bind(deadqueue).to(directexchange).with(Constant.DEAD_LETTER_QUEUE_ROUTINGKEY);
    }

}
